package org.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.example.domain.Book;
import org.example.service.BookService;
import org.example.mapper.BookMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
* @author Administrator
* @description 针对表【t_book】的数据库操作Service实现
* @createDate 2024-07-08 17:19:21
*/
@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book>
    implements BookService{

    @Resource
    private BookMapper bookMapper;
    @Override
    @Trace
    @Tag(key = "stock", value = "returnedObj")
    public Boolean stock(Integer id, Integer num) {
        //根据用户传递的图书id
        Book book = this.bookMapper.selectById(id);
        if(book!=null) {
            //判断库存是否够
            System.err.println("图书的库存是："+book.getAmount()+"购买的数量："+num);
            if(book.getAmount()>=num){
                //修改库存
                book.setAmount(book.getAmount()-num);
                this.bookMapper.updateById(book);
                return true;
            }else{
                System.err.println("库存不足！");
            }
        }

        return false;
    }
}




